package com.cash.lemoloan.networklib.Utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.cash.lemoloan.networklib.Intetface.ResultInterface;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import okhttp3.Call;
import okhttp3.MediaType;


public class NetWorkUtils {

    private static final String TAG = "NetWorkUtils";

    /**
     * 改版网络请求,传参json
     **/
    public static void postJsonOkData(Context context, String json, final String url, final ResultInterface resultInterface) {

        try {
            postJsonMethod(context, url, json, new StringCallback() {

                @Override
                public String parseNetworkResponse(okhttp3.Response response, int id) throws IOException {
                    return super.parseNetworkResponse(response, id);
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    resultInterface.onError(e.getMessage());
                }

                @Override
                public void onResponse(String msg, int id) {
                    resultInterface.onSuccess(msg, id);
                }

            });

        } catch (Exception e) {
        }
    }

    /**
     * get请求
     */
    public static void getJsonOkData(Context context, final Map<String, String> args, final String url, final ResultInterface resultInterface) {
        try {

            NetWorkUtils.GetMethod(context, url, args, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    //isShowNetError = true;
                    resultInterface.onError(e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    resultInterface.onSuccess(response, id);
                }
            });

        } catch (Exception e) {
        }
    }


    /**
     * post请求封装,传参json数据
     **/
    public static void postJsonMethod(Context context, String url, String json, StringCallback callback) {

        OkHttpUtils
                .postString()
                .tag(context)
                .url(url)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .headers(getHeaders(context))
                .build()
                .execute(callback);
    }

    /**
     * get请求封装
     **/
    public static void GetMethod(Context context, String url, Map<String, String> map, StringCallback callback) {

        OkHttpUtils
                .get()
                .url(url)
                .params(map)
                .headers(getHeaders(context))
                .build()
                .execute(callback);
    }

    private static Map getHeaders(Context context) {

        HashMap<String, String> headers = new HashMap<>();
        return headers;
    }
}
